<?php $this->JQuery()->javascriptCaptureStart()?>
$j('document').ready(function(){
    ValidateAjax.initialize('form_register_personal', '/user/validate-register-personal');
});

var uploader;
var dialog;
$j('#add_photo').click(function (){
    $j(function() {
            dialog = $j("#dialog").dialog({
                    width: 600,
                    open: function(event, ui) {
                        uploader.init();
                    }
            });
    });
});

$j(function() {

	uploader = new plupload.Uploader({
		runtimes : 'html5,flash,html4',
		browse_button : 'pickfiles',
		container : 'container_uploader',
		max_file_size : '10mb',
		url : '/default/upload/profile-pic',
		flash_swf_url : '/plupload/js/plupload.flash.swf',
		silverlight_xap_url : '/plupload/js/plupload.silverlight.xap',
		filters : [
			{title : "Image files", extensions : "jpeg,jpg,gif,png"},
			{title : "Zip files", extensions : "zip"}
		],
		resize : {width : 320, height : 240, quality : 90}
	});

	uploader.bind('Init', function(up, params) {
		$j('#filelist').html("<div>Current runtime: " + params.runtime + "</div>");
	});

	$j('#uploadfiles').click(function(e) {
		uploader.start();
		e.preventDefault();
	});

	//uploader.init();

	uploader.bind('FilesAdded', function(up, files) {
		$.each(files, function(i, file) {
			$j('#filelist').append(
				'<div id="' + file.id + '">' +
				file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
			'</div>');
		});

		up.refresh(); // Reposition Flash/Silverlight
	});

	uploader.bind('UploadProgress', function(up, file) {
		$j('#' + file.id + " b").html(file.percent + "%");
	});

	uploader.bind('Error', function(up, err) {
		$j('#filelist').append("<div>Error: " + err.code +
			", Message: " + err.message +
			(err.file ? ", File: " + err.file.name : "") +
			"</div>"
		);

		up.refresh(); // Reposition Flash/Silverlight
	});

	uploader.bind('FileUploaded', function(up, file, res) {
        var response = $.parseJSON(res.response);
		$j('#' + file.id + " b").html("100%");
        //var img = $j('img').attr("src",response.url).html();
        $j('#profile_pic_cnt').empty();
        $j(document.createElement("img"))
            .attr({ src: response.url, rel:response.ori, id:'profile_pic_to_be'})
            .appendTo($j('#profile_pic_cnt'))


	});

	// Client side form validation
	$j('#form_uploader').submit(function(e) {
		var uploader = $j('#uploader').pluploadQueue();

		// Validate number of uploaded files
		if (uploader.total.uploaded == 0) {
			// Files in queue upload them first
			if (uploader.files.length > 0) {
				// When all files are uploaded submit form
				uploader.bind('UploadProgress', function() {
					if (uploader.total.uploaded == uploader.files.length)
						$j('form').submit();
				});

				uploader.start();
			} else
				alert('You must at least upload one file.');

			e.preventDefault();
		}
	});

    $j('#validate_profile_pic').click(function() {
        $.ajax({
            type: 'POST',
            url: '<?php echo $this->geturl('confirm-profile-pic', 'user')?>',
            data: {url:$j('#profile_pic_to_be').attr('rel')},
            success:function () {
                console.log(dialog);
                $j('#dialog-thanks').fadeIn(300);
                setTimeout ( "$j(function(){dialog.dialog('destroy')})", 1000 );
                $j('#profile_profile_pic').empty();
                $j('#profile_pic_to_be').appendTo($j('#profile_profile_pic'));
                $j('#profile_pic_to_be').attr('id', 'profile_profile_pic_img');
            }
        });
    });
});
<?php $this->JQuery()->javascriptCaptureEnd()?>
<?php $this->JQuery()->addJavascriptFile('http://bp.yahooapis.com/2.4.21/browserplus-min.js')?>
<?php if (IS_LIVE):?>
    <?php $this->JQuery()->addJavascriptFile(Ac_Util::getImageBaseUrl('/js/plupload/plupload.v-'.AC_VERSION.'.js'))?>
    <?php $this->JQuery()->addJavascriptFile(Ac_Util::getImageBaseUrl('/js/plupload/plupload.html5.v-'.AC_VERSION.'.js'))?>
    <?php $this->JQuery()->addJavascriptFile(Ac_Util::getImageBaseUrl('/js/plupload/plupload.html4.v-'.AC_VERSION.'.js'))?>

    <?php $this->JQuery()->addJavascriptFile(Ac_Util::getImageBaseUrl('/js/plupload/jquery.plupload.queue.min.v-'.AC_VERSION.'.js'))?>
    <?php $this->headLink()->appendStylesheet(Ac_Util::getImageBaseUrl('/css/plupload/plupload.queue.v-'.AC_VERSION.'.css'))?>
<?php endif;?>
<?php $this->jQuery()->uiEnable()?>

<div class="grid_16">
    <?php echo $this->form;?>
</div>
